package com.xquant.trade.risk.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.xquant.trade.risk.mapper.RiskMapper;
import com.xquant.trade.trade.entity.Trade;
import com.xquant.trade.trade.entity.TradeDurationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class RiskService {
    @Autowired
    private RiskMapper riskMapper;


    /**
     * newCloud 查询业务日期 目前是当前日期
     */
    public String getBlcDate() {

        return riskMapper.getBlcDate();
    }

    /**
     * newCloud 查询最近的交易日
     */
    public String getRealBlcDate(Map param) {

        String blcDate = MapUtil.getStr(param, "calendarDate");
        List<Map> l1 = riskMapper.queryTcalendarCurrDates(param);

        return l1.size() > 0 ? StrUtil.toString(l1.get(0).get("CAL_DAY")) : blcDate;
    }

    /**
     * newCloud 查询上一个交易日
     */
    public List<Map> queryTcalendarLastDates(Map map) {
        return riskMapper.queryTcalendarLastDates(map);
    }

    /**
     * newCloud 查询当前交易日
     */
    public List<Map> queryTcalendarCurrDates(Map map) {
        return riskMapper.queryTcalendarCurrDates(map);
    }

    /**
     * newCloud 查询交易 对冲和期权
     */
    public List<Trade> queryMonitor4TradeList(Map map) {
        return riskMapper.queryMonitor4TradeList(map);
    }
    /**
     * newCloud 查询交易 对冲和期权
     */
    public List<TradeDurationDTO> queryMonitor4TradeDurationDTOList(Map map) {
        String tradeIds=MapUtil.getStr(map,"tradeIds");
        if(StrUtil.isNotEmpty(tradeIds)){
            map.put("tradeIds",tradeIds.split(","));
            map.put("calcDate",riskMapper.getBlcDate());
            return riskMapper.queryMonitor4TradeDurationDTOList(map);
        }else{
            return new ArrayList<TradeDurationDTO>();
        }

    }

    /**
     * newCloud 查询交易 对冲和期权
     */
    public List<Trade> queryScene4TradeList(Map map) {
        return riskMapper.queryMonitor4TradeList(map);
    }

    /**
     * newCloud 查询标的价格
     */
    public List getICodePrice(Map map) {
        return riskMapper.getICodePrice(map);
    }

    /**
     * newCloud 查询组合下标的
     */
    public List getUnderLyByProfit(String[] groupIds) {
        return (List) riskMapper.getUnderLyByProfit(groupIds).stream().distinct().filter(m -> m.get("productName") != null && StrUtil.isNotBlank(m.get("productName").toString())).collect(Collectors.toList());
    }

    /**
     * newCloud 查询账户下标的
     */
    public List getUnderLyByAcc(String[] accIds) {
        return (List) riskMapper.getUnderLyByAcc(accIds).stream().distinct().filter(m -> m != null && m.get("productName") != null && StrUtil.isNotBlank(m.get("productName").toString())).collect(Collectors.toList());

    }

    /**
     * newCloud 查询标的 欣欣
     */
    public List getICodeInfo(Map map) {
        return riskMapper.getICodeInfo(map);
    }
}
